package cn.buildoncloud.labor.workermanapplet.business.modules.controller;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.ProjectNotificationService;
import cn.buildoncloud.labor.workermanapplet.common.Constant;
import cn.buildoncloud.labor.workermanapplet.common.base.Result;
import cn.buildoncloud.labor.workermanapplet.common.page.Pagination;
import cn.buildoncloud.labor.workermanapplet.util.HttpUtil;
import cn.buildoncloud.labor.workermanapplet.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/projectNotification")
public class ProjectNotificationController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ProjectNotificationService notificationService;

    @Value("${webSystemIP}")
    private String webSystemIP;

    /**
     * 发送并新建项目通知
     * @param projectId
     * @param userId
     * @param title
     * @param content
     * @param showTV
     * @param startTime
     * @param endTime
     * @param remark
     * @param sender
     * @param teamIds
     * @param typeIds
     * @return
     */
    @RequestMapping("/create")
    public Object createNotification(@RequestParam(required = true) String projectId,
                                     @RequestParam(required = true) String userId,
                                     @RequestParam(required = true) String title,
                                     @RequestParam(required = false) String content,
                                     @RequestParam(required = true) boolean showTV,
                                     @RequestParam(required = false) String startTime,
                                     @RequestParam(required = false) String endTime,
                                     @RequestParam(required = false) String remark,
                                     @RequestParam(required = true) String sender,
                                     @RequestParam(required = false) String teamIds,
                                     @RequestParam(required = false) String typeIds) {
        try {

            Map<String, Object> map = new HashMap<>();
            map.put("projectId", projectId);
            map.put("title", title);
            map.put("content", content);
            map.put("showTV", showTV);
            if (StringUtil.isNotBlank(startTime)) map.put("startTime", startTime + ":00");
            if (StringUtil.isNotBlank(endTime)) map.put("endTime", endTime + ":00");
            map.put("remark", remark);
            map.put("sender", sender);
            map.put("teamIds", teamIds);
            map.put("typeIds", typeIds);
            map.put("userId", userId);
            String post = HttpUtil.post(webSystemIP + "/business/projectNotification/createNotification", map, 1000 * 30);
            return JSONObject.parseObject(post);
        } catch (Exception e) {
            log.error("projectNotification/create: ", e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

    /**
     * 项目通知列表
     *
     * @param pId          项目ID 必填
     * @param search       搜索条件
     * @param type         发送人类型
     * @param strBeginDate 发送起始时间
     * @param strEndDate   发送结束时间
     * @return
     */
    @RequestMapping("/list")
    public Object list(@RequestParam String pId,
                       @RequestParam(defaultValue = "1") Integer pageNumber,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String search,
                       @RequestParam(required = false) Integer type,
                       @RequestParam(required = false, name = "beginDate") String strBeginDate,
                       @RequestParam(required = false, name = "endDate") String strEndDate) {
        try {
            String strType = null;
            if (type != null) {
                switch (type) {
                    case 1:
                        strType = "WM";
                        break;
                    case 2:
                        strType = "WTL";
                        break;
                    case 3:
                        strType = "PA";
                        break;
                }
            }

            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date beginDate = null;
            if (StringUtils.hasText(strBeginDate)) {
                beginDate = sdf.parse(strBeginDate);
            }
            Date endDate = null;
            if (StringUtils.hasText(strEndDate)) {
                endDate = sdf.parse(strEndDate);
            }
            Pagination pms = notificationService.listProjectNotification(Long.parseLong(pId), pageNumber, pageSize, search, strType, beginDate, endDate);
            return Result.success(Constant.API_OPERATION_SUCCESS, pms);
        } catch (Exception e) {
            log.error("projectNotification/list: ", e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

    /**
     * 查看项目通知
     * @param id
     * @return
     */
    @RequestMapping("/getProjectNot")
    public Object getProjectNot(@RequestParam String id) {
        try {
            Map<String, Object> projectNot = notificationService.getProjectNotification(Long.parseLong(id));
            return Result.success(Constant.API_OPERATION_SUCCESS, projectNot);
        } catch (Exception e) {
            log.error("projectNotification/getProjectNot: ", e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

}
